<!--农直购下订单页面  2016-7-22 09:33:34-->
<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
        <title>提交订单</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> 
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="format-detection" content="telephone=no">

        <script src="__PUBLIC__/order_make/mui.min.js"></script>
        <!--[if lt IE 9]> <script src="http://cdn.bootcss.com/html5shiv/r29/html5.js"></script> <![endif]-->
        <link rel="stylesheet" type="text/css" href="__PUBLIC__/order_make/index.css" media="all">

        <!--添加警告框样式css 2016-7-22 09:47:23-->
        <link rel="stylesheet" type="text/css" href="__PUBLIC__/remind/css/alert.css" media="all">

    </head>
    <body>
        <div class="POSfixed">
            <header class="clearfix" id="headerDiv">
                <div class="back"> <a href="javascript:history.go(-1)" title="返回"><i class="pointLeft"></i></a> </div>
                <h1>提交订单</h1>
            </header>
        </div>
        <div class="container padtophasbtm">  
            <form action="http://m.51xnb.cn/servlet/XNBMallTimeLimitedSaleOrderServlet" method="get" id="secltAddressforward" style="display: none;">
                <input name="method" value="doPreSelectAddress" type="hidden">
                <input name="backUrl" value="http%3A%2F%2Fm.51xnb.cn%3A80%2Fservlet%2FBuyInShop%3Flist%3D75a5dcc7-09c1-47ff-b599-ca481bacff2e%252C1" type="hidden">
                <input id="formSubmit" value="提交" type="submit"> 
            </form>

            <div class="rows">
                <div class="rowsCon">
                    <input id="areaid" value="220602001000" type="hidden">
                    <input id="errorMsg" value="" type="hidden">
                    <input id="recAreaId" value="2b991187-00e4-41d2-89b1-bacb0a204db5" type="hidden"> 
                    <a class="change" href="javascript:;" onclick="seletAddress()">
                        <i class="pointRight pRother"></i>
                        <p><span class="name" id="contactname">行己</span><span id="contactPhone">13333444444</span></p>

                        <p id="detailAddress">吉林省_白山市_浑江区_新建街道_大多数的方式</p>
                    </a>
                </div>
            </div>

            <div class="rows">
                <h2 class="title"><span class="red">订单详情</span></h2>
            </div>

            <article class="productItem">
                <a href="javascript:;">
                    <div class="ppic">
                        <img src="__PUBLIC__/order_make/xnbmalljsonservlet" height="60" width="60">
                    </div>
                    <div class="title">
                        <h3>康佳 BCD-288GY4S 288升 多门电冰箱 </h3>                
                        <span class="price" id="75a5dcc7-09c1-47ff-b599-ca481bacff2e">1699.00</span>                              
                        <p> <span class="floatRight">x1</span></p>
                    </div>
                </a>
            </article>

            <div class="producthm">
                <p>买家留言</p>
                <div class="messages"><textarea id="comments" placeholder="买家留言.."></textarea></div>
            </div>

            <!--            <div class="rows">
                            <h2 class="title"><span class="red">优惠券</span></h2>
                            <div class="rowsCon">
                                <div class="juancon">
                                    <p id="coupon-list">
                                        <span>
                                            <input value="" id="coupon_01" name="couponCode" class="coupon-text input-text iptext" placeholder="请输入优惠券编码" type="text">
                                            <span class="rightbtn" id="coupon_01_img_000" style="display: none ! important;"></span>
                                            <br><span class="wrongbtn" id="coupon_01_img_001" style="display: none ! important;">无效优惠券码</span>
                                        </span>
                                    </p>
                                    <p><a href="javascript:;" class="ipbtn" id="add_coupon">+添加优惠券</a></p>
                                </div>
                            </div>
                        </div>-->

            <div class="rows">
                <h2 class="title"><span class="red">礼券</span></h2>
                <div class="rowsCon">

                    <div style="font-size: 12px;">  
                        <div class="dcleckbox">
                            <input checked="checked" id="userGiftCardFlag" name="CheckboxGroup" class="regular-checkbox" type="checkbox">
                            <label for="userGiftCardFlag" onclick="userGificardChange();" style="vertical-align: middle;"></label>&nbsp;礼券抵扣:
                            <div class="reds" style="width: 40px;">0.00</div>元 
                            <span style="float: right; margin-top: 3px; margin-left: 30px;">礼券余额:<span class="reds" style="width: 70px;">3</span>元</span>
                        </div>
                    </div>

                </div>
            </div>

            <div class="rows">
                <h2 class="title"><span class="red">应付金额</span></h2>
                <div class="rowsCon">
                    <span id="freightFee" style="display: none;">0</span>
                    <span id="goodmount" style="display: none;">1699.00</span>
                    <p>商品金额 <span class="floatRight reds" id="goodmount_freightFee">￥1699.00</span></p>
                    <!--<p>优惠券 <span class="floatRight reds"><span class="floatLeft">-</span>￥<span id="usedCoupons">0.00</span></span></p>-->
                    <p>礼券 <span class="floatRight reds"><span class="floatLeft">-</span>￥<span id="gifcardAmount">0.00</span></span></p>
                </div>
            </div>   
        </div>

        <div class="suspension">
            <span>应付金额:￥<span class="colr" id="amountToBePaid"> 1699.00</span></span>       
            <a class="acc_upload " href="javascript:;" onclick="submitOrder(this)">提交订单</a>
        </div>

        <script src="__PUBLIC__/order_make/jquery.min.js"></script>
        <script src="__PUBLIC__/order_make/jquery.alerts.js"></script>
        <script src="__PUBLIC__/order_make/jquery-ui.min.js"></script>

        <!--        <script>
                        $(function () {
                            //地区选择框 初始化
                            $("#waitDialog").dialog({
                                autoOpen: false,
                                modal: true,
                                width: '90%',
                            });
                            $(".aclose").click(function () {//返回
                                $("#dialog").dialog("close");
                            });
                            $(".sure").click(function () {//确定
                                submitSubscription();
                                $("#dialog").dialog("close");
                            });
                            $("#dj").click(function () {
                                $("#dialog").dialog("open");
                            });
                        });
                </script>-->

        <script type="text/javascript">
                var couponRowNum = 1;
                var myCoupons = [];//全局变量我的优惠劵；

                $(function () {
                    //ev_fre();
                    initBind();
//                    doLoadMyCoupons();
                });

                $(function () {
                    $('label').click(function () {
                        var radioId = $(this).attr('name');
                        $('label').removeAttr('class') && $(this).attr('class', 'checked');
                        $('input[type="radio"]').removeAttr('checked') && $('#' + radioId).attr('checked', 'checked');
                    });

                    $('#comments').bind({
                        focus: function () {
                            if (this.value == this.defaultValue) {
                                this.value = "";
                            }

                        },
                        blur: function () {
                            if (this.value == "" || this.value == this.defaultValue) {
                                this.value = this.defaultValue;
                            }
                        }
                    });

                    // 添加“优惠卷”输入框
                    $('#add_coupon').click(function () {
                        couponRowNum += 1;
                        var contentTrHtml = "<span>";
                        contentTrHtml += "<input type='text' id='coupon_" + couponRowNum + "' name='couponCode' class='coupon-text input-text iptext' placeholder='请输入优惠券编码'/>";
                        contentTrHtml += "<span class='rightbtn' id='coupon_" + couponRowNum + "_img_000' style='display:none'></span>";
                        contentTrHtml += "<br><span class='wrongbtn' id='coupon_" + couponRowNum + "_img_001' style='display:none'>无效优惠券码</span>";
                        contentTrHtml += "</span>";

                        $("#coupon-list").append(contentTrHtml);
                        initBind();
                        //$("#coupon_" + couponRowNum).focus();//立即获取焦点
                    });

                    if ($("#areaid").val() == null || $("#areaid").val() == '') {
                        jAlert("您没有选择地址，并且也没有收货地址，暂时无法计算费用~", "");
                    }
                    if ($("#errorMsg").val() != null && $("#errorMsg").val().length > 0) {
                        $.alerts.okButton = "返回";
                        jAlert($('#errorMsg').val(), "");
                    }
                });

                function initBind() {
                    $('.coupon-text').bind({
                        focus: function () {
                            if (this.value == this.defaultValue) {
                                this.value = "";
                            }

                            var $parent = $(this).parent();
                            $(".renav").remove();
                            if (myCoupons != null && myCoupons.length > 0) {
                                var tempCodes = [];
                                var couponCodes = $("input[name='couponCode']");
                                for (var i = 0; i < myCoupons.length; i++) {
                                    var isExist = false;
                                    var code = myCoupons[i].code;
                                    for (var j = 0; j < couponCodes.length; j++) {
                                        if (code == couponCodes[j].value) {
                                            isExist = true;
                                            break;
                                        }
                                    }
                                    if (!isExist) {
                                        tempCodes.push(myCoupons[i]);
                                    }
                                }

                                if (tempCodes.length > 0) {
                                    var inputId = this.id;
                                    var imgid = this.id + "_img";
                                    var content = "<div class='renav'><ul style='margin-top: 0px;'>";
                                    jQuery.each(tempCodes, function (i, tempCoupon) {
                                        content += "<li ><a onclick='doSelectCoupons(\"" + inputId + "\",\"" + imgid + "\",\"" + tempCoupon.code + "\");'><img src='http://m.51xnb.cn:80/mall/lib/images/select.png'  height='15'width='15';><span>" + tempCoupon.name + "</span></a></li>";
                                    });
                                    content += "</ul> </div>";
                                    $parent.append(content);
                                }
                            }

                        },
                        blur: function () {
                            var imgid = this.id + "_img";
                            if (this.value == "" || this.value == this.defaultValue) {
                                this.value = this.defaultValue;
                                //$("#" + imgid).hide();
                                $("#" + imgid + "_000").hide();//000-正确
                                $("#" + imgid + "_001").hide();//001-错误
                            } else {
                                //checkCoupon(imgid,this.value);
                                reCalculateOrderAmount(imgid);
                            }
                        }
                    });
                }

                function doSelectCoupons(inputid, imgid, couponCode) {
                    $(".renav").remove();
                    $("#" + inputid).val(couponCode);
                    reCalculateOrderAmount(imgid);
                }
//            function doLoadMyCoupons() {
//
//                try {
//                    var url = "http://m.51xnb.cn:80/servlet/XNBMallJSONServlet";
//                    var data = {method: "queryMyCoupons", pageIndex: 0, pageSize: 9999};
//                    $.ajax({
//                        type: "POST",
//                        url: url,
//                        dataType: "json",
//                        data: data,
//                        success: function (obj) {
//                            try {
//                                if (obj == null || obj.result == null || obj.code != "200"
//                                        || (obj.result.total == 0 || obj.result.total == null)
//                                        || (obj.result.result == null || obj.result.result.length == 0)) {//全部加载完了
//                                    //没有优惠券
//                                } else {//成功新一页的订单
//                                    //插入新的一页优惠券数据到页面
//                                    myCoupons = obj.result.result;
//                                }
//                            } catch (e00) {
//                            }
//                        },
//                        error: function (errs) {
//                        }
//                    });
//                } catch (e0) {
//                }
//            }

                //验证优惠卷
//            function checkCoupon(imgid, value) {
//                var url = 'http://m.51xnb.cn:80/servlet/XNBMallOrderServlet';
//                $.ajax({
//                    type: "POST",
//                    url: url,
//                    dataType: "json",
//                    data: {
//                        method: 'verifyCoupon',
//                        goodsNumsJson: $("#goodsnumsJoson").val(),
//                        couponCode: value
//                    },
//                    success: function (obj) {
//                        if (obj != null) {
//                            if (obj.code == 'ok') {
//                                //$("#" + imgid).show();
//                                $("#" + imgid + "_000").show();//000-正确
//                                $("#" + imgid + "_001").hide();//001-错误
//                                reCalculateOrderAmount();
//                            } else {
//                                $("#error_msg").html(obj.errorMsg);
//                                $('#error-dialog').modal('show');
//                            }
//                        }
//                    },
//                    error: function (errs) {
//                        jAlert("网络不太稳定，再试试看", "");
//                    }
//                });
//            }

                //重新计算订单金额
//            function reCalculateOrderAmount(imgid) {
//                var areaid = $("#areaid").val();
//                var serviceStation = $("input[name='serviceStation']:checked").val();
//                var goodsNumsJson = $("#goodsnumsJoson").val();
//                var couponIds = [];
//                var couponCodes = $("input[name='couponCode']");
//                jQuery.each(couponCodes, function (i, couponCode) {
//                    if (couponCode.value != couponCode.defaultValue && couponCode.value != '') {
//                        couponIds.push(couponCode.value);
//                    }
//                });
//                var couponIdsjoson = JSON.stringify(couponIds);
//                var url = 'http://m.51xnb.cn:80/servlet/XNBMallOrderServlet';
//                $.ajax({
//                    type: "POST",
//                    url: url,
//                    dataType: "json",
//                    data: {
//                        method: "calculateOrderAmount",
//                        areaId: areaid,
//                        sSId: serviceStation,
//                        goodsNumsJson: goodsNumsJson,
//                        couponIdsjoson: couponIdsjoson
//                    },
//                    success: function (obj) {
//                        if (obj != null) {
//                            if (obj.code == "ok") {
//                                if (imgid != null && imgid != '') {
//                                    //$("#" + imgid).show();
//                                    $("#" + imgid + "_000").show();//000-正确
//                                    $("#" + imgid + "_001").hide();//001-错误
//                                }
//                                var settleInfo = obj.settleInfo;
//                                $("#goodmount").html((settleInfo.amount).toFixed(2));
//                                $("#freightFee").html((settleInfo.freightFee).toFixed(2));
//                                $("#usedCoupons").html((settleInfo.usedCoupons).toFixed(2));
//                                $("#rebates").html((settleInfo.rebates).toFixed(2));
//                                $("#amountToBePaid").html((settleInfo.amountToBePaid).toFixed(2));
//                                $("#goodmount_freightFee").html((settleInfo.amount + settleInfo.freightFee).toFixed(2));
//                                jQuery.each(settleInfo.goodsPriceAndFees, function (key, value) {
//                                    $("#" + key).html(value.toFixed(2));
//                                });
//                            } else {
//                                if (/^error\_\d{3}\_\w*/.test(obj.errorMsg)) {//示例：e001_无效码
//                                    var mymsgs = obj.errorMsg.split("_");
//                                    $("#" + imgid + "_000").hide();//000-正确
//                                    $("#" + imgid + "_errorMsg").text(mymsgs[2]);//错误提示
//                                    $("#" + imgid + "_001").show();//001-错误
//                                } else {
//                                    $("#error_msg").html(obj.errorMsg);
//                                    $("#error-dialog").modal('show');
//                                    //alert(obj.errorMsg);
//                                }
//                            }
//                        }
//                    },
//                    error: function (errs) {
//                        jAlert("网络不太稳定，再试试看", "");
//                    }
//                });
//            }

                //使用礼品卡，重新计算金额
                function userGificardChange() {
                    var areaid = $("#areaid").val();
                    var userGiftCardFlag = document.getElementById("userGiftCardFlag").checked;
                    var serviceStation = $("input[name='serviceStation']:checked").val();
                    var goodsNumsJson = $("#goodsnumsJoson").val();
                    var couponIds = [];
                    var couponCodes = $("input[name='couponCode']");
                    jQuery.each(couponCodes, function (i, couponCode) {
                        if (couponCode.value != couponCode.defaultValue && couponCode.value != '') {
                            couponIds.push(couponCode.value);
                        }
                    });
                    var couponIdsjoson = JSON.stringify(couponIds);
                    var url = 'http://m.51xnb.cn:80/servlet/XNBMallOrderServlet';
                    var serviceShopId = $("#serviceShopId").val();
                    $.ajax({
                        type: "POST",
                        url: url,
                        dataType: "json",
                        data: {
                            method: "calculateOrderAmount",
                            areaId: areaid,
                            sSId: serviceStation,
                            goodsNumsJson: goodsNumsJson,
                            couponIdsjoson: couponIdsjoson,
                            userGiftCardFlag: !userGiftCardFlag,
                            serviceShopId: serviceShopId
                        },
                        success: function (obj) {
                            if (obj != null) {
                                if (obj.code == "ok") {
                                    var settleInfo = obj.settleInfo;
                                    $("#goodmount").html((settleInfo.amount).toFixed(2));
                                    $("#freightFee").html((settleInfo.freightFee).toFixed(2));
                                    $("#usedCoupons").html((settleInfo.usedCoupons).toFixed(2));
                                    $("#amountToBePaid").html((settleInfo.amountToBePaid).toFixed(2));
                                    $("#goodmount_freightFee").html((settleInfo.amount + settleInfo.freightFee).toFixed(2));
                                    jQuery.each(settleInfo.goodsPriceAndFees, function (key, value) {
                                        $("#" + key).html(value.toFixed(2));
                                    });
                                    if (settleInfo.useGiftCardFlag) {
                                        $("#gifcardAmount").html(settleInfo.freightGiftCard.toFixed(2));
                                    } else {
                                        $("#gifcardAmount").html("0.00");
                                    }
                                }
                            }
                        },
                        error: function (errs) {
                            jAlert("重新计算金额出错，请刷新重试~", "");
                        }
                    });
                }


                function submitOrder(btn) {

                    btn.setAttribute("disabled", true);
                    var raid = $("#recAreaId").val();
                    var commentsObj = $("#comments");
                    var comments = commentsObj.val();

                    if (commentsObj.val() == '给卖家留言...') {
                        comments = "";
                    }

                    if ($("#errorMsg").val() != null && $("#errorMsg").val().length > 0) {
                        jAlert($("#errorMsg").val(), "");
                        return;
                    }

                    if (raid == null || raid == '') {
                        jAlert("给个地址吧~", "");
                        return;
                    }

                    var goodsAmount = $("#goodmount").html();
                    var gifcardAmount = $("#gifcardAmount").html();
                    var usedCoupons = $("#usedCoupons").html();
                    var couponIds = "";
                    var couponCodes = $("input[name='couponCode']");
                    $("#waitDialog").dialog("open");
                    jQuery.each(couponCodes, function (i, couponCode) {
                        if (couponCode.value != couponCode.defaultValue && couponCode.value != '') {
                            if (couponIds.length > 0) {
                                couponIds += ",";
                            }
                            couponIds += couponCode.value;
                        }
                    });

                    var userGiftCardFlag = $("#userGiftCardFlag").get(0);
                    if (!userGiftCardFlag.checked) {
                        gifcardAmount = 0;
                    }

                    var url = 'http://m.51xnb.cn:80/servlet/OrderServlet';
                    $.ajax({
                        type: "POST",
                        url: url,
                        dataType: "json",
                        data: {
                            method: 'doBuyInShop',
                            raid: raid,
                            list: '75a5dcc7-09c1-47ff-b599-ca481bacff2e%2C1',
                            coupons: couponIds,
                            giftcard: gifcardAmount,
                            goodsAmount: goodsAmount,
                            usedCoupons: usedCoupons,
                            comments: comments,
                            giftcardFreight: gifcardAmount
                        },
                        success: function (obj) {
                            $("#waitDialog").dialog("close");
                            if (obj != null) {
                                if (obj.code == 'ok') {
                                    window.location.href = "http://m.51xnb.cn:80/mall/member/payMethod.jsp?orderId=" + obj.orderId + "";
                                } else {
                                    if (/^error\_\d{3}\_\w*/.test(obj.msg)) {
                                        //示例：e001_无效码
                                        var mymsgs = obj.msg.split("_");
                                        $("#" + imgid + "_000").hide();//000-正确
                                        $("#" + imgid + "_errorMsg").text(mymsgs[2]);//错误提示
                                        $("#" + imgid + "_001").show();//001-错误
                                    } else {
                                        btn.removeAttribute("disabled");
                                        jAlert(obj.msg, "");
                                    }
                                }
                            }
                        },
                        error: function (errs) {
                            $("#waitDialog").dialog("close");
                            btn.removeAttribute("disabled");
                            jAlert("网络不太稳定，再试试看", "");
                        }
                    });

                }

                function seletAddress() {
                    $("#secltAddressforward").submit();
                }
        </script>

        <div aria-labelledby="ui-id-1" aria-describedby="waitDialog" role="dialog" tabindex="-1" style="display: none; position: fixed;" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable">
            <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-draggable-handle">
                <span class="ui-dialog-title" id="ui-id-1">等待框</span>
                <button title="Close" role="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" type="button">
                    <span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span>
                    <span class="ui-button-text">Close</span>
                </button>
            </div>
            <div class="ui-dialog-content ui-widget-content" id="waitDialog" style="">
                <div class="sui-select" style="margin-top: 11%; text-align: center;">
                    <label style="font-size: 20px;">请等待提交完成...</label>
                </div>
            </div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-n"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-e"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-s"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-w"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-sw"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-ne"></div>
            <div style="z-index: 90;" class="ui-resizable-handle ui-resizable-nw"></div>

        </div>
    </body>
</html>
